package com.middleland.mobile.rccar.bt;

import java.io.IOException;
import java.util.UUID;

import com.middleand.mobile.rccar.logger.Log;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


public class BtConnectThread extends Thread {

	final static String TAG = "BtConnectThread";
	private final BluetoothSocket mmSocket;
	private final BluetoothDevice mmDevice;
	private String mSocketType;
	private Handler mHandler;
	private BtConnectedThread mConnectedThread;
	private BluetoothAdapter mAdapter;

	public BtConnectThread(BluetoothDevice device, boolean secure,
			Handler mHandler, BluetoothAdapter adapter, UUID MY_UUID_SECURE) {

		mmDevice = device;
		BluetoothSocket tmp = null;
		mSocketType = secure ? "Secure" : "Insecure";
		this.mAdapter = adapter;

		// Get a BluetoothSocket for a connection with the
		// given BluetoothDevice
		try {
			tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);

		} catch (IOException e) {
			Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
		}
		mmSocket = tmp;
	}

	public void run() {
		Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
		setName("ConnectThread" + mSocketType);

		// Always cancel discovery because it will slow down a connection
		mAdapter.cancelDiscovery();

		// Make a connection to the BluetoothSocket
		try {
			// This is a blocking call and will only return on a
			// successful connection or an exception
			mmSocket.connect();
		} catch (IOException e) {
			// Close the socket
			try {
				mmSocket.close();
			} catch (IOException e2) {
				Log.e(TAG, "unable to close() " + mSocketType
						+ " socket during connection failure", e2);
			}

			return;
		}

		// Start the connected thread
		connected(mmSocket, mmDevice, mSocketType);
	}

	/**
	 * Start the ConnectedThread to begin managing a Bluetooth connection
	 * 
	 * @param socket
	 *            The BluetoothSocket on which the connection was made
	 * @param device
	 *            The BluetoothDevice that has been connected
	 */
	public synchronized void connected(BluetoothSocket socket,
			BluetoothDevice device, final String socketType) {
		Log.d(TAG, "connected, Socket Type:" + socketType);

		// Start the thread to manage the connection and perform transmissions
		mConnectedThread = new BtConnectedThread(socket, socketType, mHandler);
		mConnectedThread.start();

		// Send the name of the connected device back to the UI Activity
		Message msg = mHandler.obtainMessage(BluetoothChat.MESSAGE_DEVICE_NAME);
		Bundle bundle = new Bundle();
		bundle.putString(BluetoothChat.DEVICE_NAME, device.getName());
		msg.setData(bundle);
		mHandler.sendMessage(msg);

	}

	public void cancel() {
		try {
			mmSocket.close();
		} catch (IOException e) {
			Log.e(TAG, "close() of connect " + mSocketType + " socket failed",e);
		}
	}
}